<template>
  <div>
    <n-card :bordered="false" class="proCard">
      <n-form
        :label-width="200"
        :model="formValue"
        :rules="rules"
        label-placement="left"
        ref="formRef"
        class="py-8"
      >
        <n-grid cols="2" responsive="screen">
          <n-grid-item offset="0">
            <n-form-item label="登记人" path="registrant">
              <n-input disabled v-model:value="formValue.registrant" placeholder="登记人" />
            </n-form-item>
            <n-form-item label="劳务公司名称" path="name">
              <n-input placeholder="劳务公司名称" v-model:value="formValue.name" />
            </n-form-item>
            <n-form-item label="开户银行" path="bank">
              <n-input v-model:value="formValue.bank" placeholder="开户银行" />
            </n-form-item>
            <n-form-item label="联系人" path="contacts">
              <n-input placeholder="联系人" v-model:value="formValue.contacts" />
            </n-form-item>
          </n-grid-item>
          <n-grid-item offset="0">
            <n-form-item label="所属部门" path="department">
              <n-input placeholder="所属部门" disabled v-model:value="formValue.department" />
            </n-form-item>
            <n-form-item label="劳务公司类型" path="company_type">
              <n-input placeholder="劳务公司类型" v-model:value="formValue.company_type" />
            </n-form-item>
            <n-form-item label="银行账号" path="bank_number">
              <n-input placeholder="银行账号" v-model:value="formValue.bank_number" />
            </n-form-item>
            <n-form-item label="联系人电话" path="contact_mobile">
              <n-input placeholder="联系人电话" v-model:value="formValue.contact_mobile" />
            </n-form-item>
          </n-grid-item>
        </n-grid>
        <n-form-item label="公司简介" path="brief">
          <n-input v-model:value="formValue.brief" placeholder="公司简介" type="textarea" />
        </n-form-item>
        <n-form-item label="备注" path="remarks">
          <n-input v-model:value="formValue.remarks" placeholder="备注" type="textarea" />
        </n-form-item>
        <n-form-item label="附件上传" path="files">
          <uploadFiles
            action="https://example.com/upload"
            accept="image/*"
            multiple
            :limit="5"
            :maxFileSize="5"
            placeholder="点击或拖拽图片到此处上传（最多5张，每张不超过5MB）"
            v-model="formValue.files"
          />
        </n-form-item>
      </n-form>
    </n-card>
    <n-space justify="center">
      <n-button type="warning" @click="formSubmit">存草稿</n-button>
      <n-button type="primary" @click="formSubmit">提交</n-button>
      <n-button @click="resetForm">重置</n-button>
    </n-space>
  </div>
</template>

<script lang="ts" setup>
  import { ref, unref, reactive, onMounted } from 'vue';
  import { useMessage } from 'naive-ui';
  import { ResultEnum } from '@/enums/httpEnum';
  import {
    getLaborCompany,
    patchLaborCompany,
    putLaborCompany,
  } from '@/api/laborCompany/laborCompany';
  import uploadFiles from '@/components/uploadFiles.vue';
  import { useRouter } from 'vue-router';
  import { useUser } from '@/store/modules/user';
  const userStore = useUser();
  const userInfo = userStore.getUserInfo;

  const id: any = ref(useRouter().currentRoute.value.params.id);

  const rules = {
    name: {
      required: true,
      message: '请输入劳务公司名称',
      trigger: 'blur',
    },
    bank: {
      required: true,
      message: '请输入开户银行',
      trigger: 'blur',
    },
    contacts: {
      required: true,
      message: '请输入联系人',
      trigger: ['input'],
    },
    company_type: {
      required: true,
      message: '请选择劳务公司类型',
      trigger: ['blur', 'change'],
    },
    bank_number: {
      required: true,
      message: '请输入银行账号',
      trigger: 'change',
    },
    contact_mobile: {
      required: true,
      message: '请输入联系人电话',
      trigger: 'change',
    },
  };

  const formRef: any = ref(null);
  const message = useMessage();

  const defaultValueRef = () => ({
    id: 0,
    admin_id: 0,
    registrant: '',
    name: '',
    bank: '',
    contacts: '',
    department: '',
    company_type: '',
    bank_number: '',
    contact_mobile: '',
    brief: '',
    remarks: '',
    files: '',
  });

  let formValue = reactive(defaultValueRef());
  function formSubmit() {
    formRef.value.validate(async (errors) => {
      if (!errors) {
        let response: any = null;
        if (formValue.id == 0) {
          response = await putLaborCompany(formValue);
        } else {
          response = await patchLaborCompany(formValue);
        }

        if (response.code == ResultEnum.SUCCESS) {
          message.success('操作成功');
          formValue.id = response.data;
          getData();
        }
        message.success('验证成功');
      } else {
        message.error('验证失败，请填写完整信息');
      }
    });
  }

  function resetForm() {
    formRef.value.restoreValidation();
    formValue = Object.assign(unref(formValue), defaultValueRef());
  }

  const getData = async () => {
    const response: any = await getLaborCompany(formValue.id);
    if (response.code === ResultEnum.SUCCESS) {
      response.data.price = parseFloat(response.data.price);
      Object.assign(formValue, response.data);
    }
  };

  onMounted(() => {
    formValue.id = id.value;
    formValue.admin_id = userInfo.id
    formValue.registrant = userInfo.name
    if(userInfo.company.role){
      formValue.department = userInfo.company.role.name
    }
    formValue.role_id = userInfo.company.role_id
    if (formValue.id != 0) {
      getData();
    }
  });
</script>
